//
//  WHRInDiagnoseVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/12/18.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "WHRInDiagnoseVC.h"
#import "WHRInDiagnoseCell.h"
#import "WHRInDiagnoseModel.h"
#import "UITableView+SDAutoTableViewCellHeight.h"


@interface WHRInDiagnoseVC ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic , strong) UITableView *tableView;
@property (nonatomic , strong) NSMutableArray<WHRInDiagnoseModel *> *modelsArray;
@property (nonatomic , assign) NSInteger pageIndex;
@property (nonatomic , strong) NSString *fFlag;



@end

@implementation WHRInDiagnoseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self creatView];
    [self setlayout];
    
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self requestList];
        
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";
    
    [self.tableView.mj_header beginRefreshing];
    
    
}

- (void)creatView {
    
    _tableView = [[UITableView alloc]init];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerClass:[WHRInDiagnoseCell class] forCellReuseIdentifier:[WHRInDiagnoseCell reuseID]];;
    [self.view addSubview:_tableView];
    
    
}

- (void)setlayout {
    
    _tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(SCREEN_WIDTH)
    .heightIs(MyTableViewHeight);
    
    
}


#pragma mark - requestNetwork

- (void)requestList {
    
    ZX_WEAK_SELF
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    
    params[@"fUserId"] = @"zzh001";
    params[@"fStatus"] = @"1";
    params[@"pageSize"] = @15;
    params[@"pageNum"] = @(self.pageIndex);
    
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_AgricultureCircleProfessorDiagnose, URL_AgricultureCircleProfessorDiagnose_selectStartQuestionPage) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [WHRInDiagnoseModel  mj_objectArrayWithKeyValuesArray:respose[@"body"][@"data"][@"list"]];
            
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
        }
        
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } failure:^(NSError *error) {
        
        if (self.pageIndex > 1) {
            self.pageIndex--;
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
    
    
    
    
    
}

#pragma mark - tableViewDelegate & tableViewDatasource

//自适应第二步

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[WHRInDiagnoseCell class] contentViewWidth:SCREEN_WIDTH];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    
    WHRInDiagnoseCell *cell = [tableView dequeueReusableCellWithIdentifier:[WHRInDiagnoseCell reuseID] forIndexPath:indexPath];
    //ZX_WEAK_CELL(cell)
    cell.model = self.modelsArray[indexPath.row];
    self.fFlag = cell.model.fFlag;
  
    
    /** 查看诊断按钮的点击方法 */
    cell.tapCelllickedBlock = ^(NSInteger index) {
        
     
        
    };
    
    if ([self.fFlag isEqualToString:@"0"]) {
        
        cell.titleLabel.text =[NSString stringWithFormat:@"作物名称:%@", cell.model.fTitle];
     
        [cell.diagnoseBtn setTitle:cell.model.fStatusCHS forState:UIControlStateNormal];
        cell.diagnoseBtn.backgroundColor = COLOR_ORG;
       
    
        
    }
    
    if ([self.fFlag isEqualToString:@"1"]) {
        
        cell.titleLabel.text =[NSString stringWithFormat:@"咨询专家:%@", cell.model.fExpertName];
        
        cell.rewardBtn.hidden = YES;
        cell.priceLabel.hidden = YES;
       
//        cell.diagnoseBtn.sd_layout
//        .rightSpaceToView(cell.contentView, 10)
//        .topSpaceToView(cell.contentView, 10)
//        .widthIs(40)
//        .heightIs(30);
//        
//        
//        cell.titleLabel.sd_layout
//        .leftSpaceToView(cell.contentView, 10)
//        .rightSpaceToView(cell.diagnoseBtn, 10)
//        .topSpaceToView(cell.contentView, 10)
//        .heightIs(20);
//        
//        
//         cell.startTimeLabel.sd_layout
//        .leftEqualToView(cell.textLabel)
//        .rightSpaceToView(cell.diagnoseBtn, 10)
//        .topSpaceToView(cell.titleLabel, 10)
//        .heightIs(20);
    
    }
    
    return cell;
    
}


@end
